package com.jy.modules.cims.sysassist;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.sysUtils.DateTimeUtils;
import com.jy.modules.cims.common.sysUtils.SearchParamUtil;
import com.jy.modules.cims.data.common.dao.TCMmQueueDao;
import com.jy.modules.cims.data.dao.CreditRightDao;
import com.jy.modules.cims.data.dao.DueExecuteTaskDao;
import com.jy.modules.cims.data.dao.SysAssistDao;
import com.jy.modules.cims.data.dto.DueExecuteTaskCfgDto;
import com.jy.modules.cims.data.dto.InteractClassInvokeDto;
import com.jy.modules.cims.data.dto.InteractClassParamterDto;
import com.jy.modules.cims.interact.InteractClassInvokeService;
import com.jy.modules.cims.interact.LoggingService;

/**
 * <b>类名称：</b>DeleteHistoryDataService<br/>
 * <b>类描述：实现删除历史数据处理服务。</b><br/>
 * <b>创建人：</b>shengzhoutao<br/>
 * <b>修改人：</b><br/>
 * <b>修改时间：</b>2015-4-1 上午10:00:00<br/>
 * <b>修改备注：</b><br/>
 * <b>版本信息：</b>v1.0.0<br/>
 */
@Service("com.jy.modules.cims.sysassist.DeleteHistoryDataService")
public class DeleteHistoryDataService {
	private static final Logger logger = LoggerFactory.getLogger(DeleteHistoryDataService.class);
	
	@Autowired
	public DueExecuteTaskDao dueExecuteTaskDao;
	
	@Autowired
	public TCMmQueueDao queueDao;

	@Autowired
	public SysAssistDao sysAssistDao;

	@Autowired
	private CreditRightDao creditRightDao;
	
	@Autowired
	@Qualifier("com.jy.modules.cims.interact.InteractClassInvokeService")
	public InteractClassInvokeService interactClassInvokeService;

	@Autowired
	@Qualifier("com.jy.modules.cims.interact.LoggingService")
	public LoggingService loggingService;

	/**
	 * @Description 删除指定日期的若干天数以前(包含指定日期)的LoggingEvent信息
	 * @param date
	 *            指定日期参数
	 * @param days
	 *            向前历史的日期数
	 * @throws Exception
	 * @author shengzhoutao
	 */
	public void deleteInvalidLoggingEvent(Date historyDate, int days) throws Exception {
		loggingService.deleteInvalidLoggingEvent(historyDate, days);
	}

	/**
	 * @Description 删除指定日期的若干天数以前(包含指定日期)的执行接口信息，以及接口对应的参数信息。
	 * @param date
	 *            指定日期参数
	 * @param days
	 *            向前历史的日期数
	 * @throws Exception
	 * @author shengzhoutao
	 */
	public void deleteHistoryInteractClassInvoke(Date historyDate, int days) throws Exception {
		interactClassInvokeService.deleteHistoryInteractClassInvoke(historyDate, days);
	}

	/**
	 * @Description 删除指定日期的若干天数以前(包含指定日期)的到期执行任务表信息，以及接口对应的参数信息。
	 * @param date
	 *            指定日期参数
	 * @param days
	 *            向前历史的日期数
	 * @throws Exception
	 * @author shengzhoutao
	 */
	public void deleteDueExecuteTaskCfg(Date historyDate, int days) throws Exception {
		historyDate = DateTimeUtils.addDays(historyDate, -days);
		DueExecuteTaskCfgDto dueExecuteTaskCfgDto = new DueExecuteTaskCfgDto();
		dueExecuteTaskCfgDto.setDuedate(new java.sql.Timestamp(historyDate.getTime()));

		dueExecuteTaskDao.deleteInteractClassParamter(SearchParamUtil.getSearchParam(dueExecuteTaskCfgDto));
		dueExecuteTaskDao.deleteDueExecuteTaskCfg(SearchParamUtil.getSearchParam(dueExecuteTaskCfgDto));

		dueExecuteTaskCfgDto = null;
	}
	
	/**
	 * @Description 删除临时表存放历史数据。
	 * @throws Exception
	 * @author shengzhoutao
	 */
	public void deleteHistoryData() throws Exception {
		try {
			sysAssistDao.deleteHistoryDueExecuteTaskCfg(SearchParamUtil.getSearchParam(new DueExecuteTaskCfgDto()));
		} catch (Exception e) {
			logger.error("deleteHistoryDueExecuteTaskCfg出现异常。", e);
		}
		
		try {
			sysAssistDao.deleteHistoryInteractClassParamter(SearchParamUtil.getSearchParam(new InteractClassParamterDto()));
		} catch (Exception e) {
			logger.error("deleteHistoryInteractClassParamter出现异常。", e);
		}
		
		try {
			sysAssistDao.deleteHistoryInteractClassInvoke(SearchParamUtil.getSearchParam(new InteractClassInvokeDto()));
		} catch (Exception e) {
			logger.error("deleteHistoryInteractClassInvoke出现异常。", e);
		}
		
		try {
			sysAssistDao.deleteHistoryQuota(null);
		} catch (Exception e) {
			logger.error("deleteHistoryQuota出现异常。", e);
		}
		
		try {
			sysAssistDao.deleteHistoryDealInterface(null);
		} catch (Exception e) {
			logger.error("deleteHistoryDealInterface出现异常。", e);
		}
		
		try {
			queueDao.deleteHistoryTCMmQueue(null);
		} catch (Exception e) {
			logger.error("deleteHistoryTCMmQueue出现异常。", e);
		}
		
		try {
			creditRightDao.deleteInvalidCreditDetail(null);
		} catch (Exception e) {
			logger.error("deleteInvalidCreditDetail出现异常。", e);
		}
		
		try {
			creditRightDao.deleteInvalidCreditDeal(null);
		} catch (Exception e) {
			logger.error("deleteInvalidCreditDeal出现异常。", e);
		}
	}
}
